﻿
#region Usings

using FasterWPF;
using FasterWPF.Generators;
using FasterWPFDemo.UserControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

#endregion

namespace FasterWPFDemo
{
    //DockPanel
    public partial class TabItem06 : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        public TabItem06()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.

            this.Initialize(ContainerType.VerticalPanel);

            this.Width = 750D;
            this.Height = 750D;

            this.BeginSettings<UserControl>().SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3))
                       .SetSelectorClass<UserControl>(0, 0, "tabLabel")
                       .SetFontOnLabel<UserControl>(0, 0, "Segoe UI", 12D, FontWeights.Normal, FontStyles.Normal)
                       .SetTextColorForLabel<UserControl>(0, 0, Brushes.White)
                       .EndSettings();
        }

        public void LoadUserControl()
        { 
            string dockguid0 = Guid.NewGuid().ToString();
            string dockguid1 = Guid.NewGuid().ToString();
            string dockguid2 = Guid.NewGuid().ToString();

            DockPanel myDockPanel = null;
            myDockPanel = new DockPanel();

            myDockPanel.Initialize(double.NaN, double.NaN, ContainerType.Grid, 5, 3);
            myDockPanel.SetCompositeGridDimensions<DockPanel>(250D, 250D, 3, 3, 0.33, GridUnitType.Auto, 0.3, GridUnitType.Auto);
            myDockPanel.LastChildFill = false;
            myDockPanel.Background = Brushes.White;

            myDockPanel.BeginSettings<DockPanel>()
                       .SetItemBorderSettings<DockPanel>(0, 0, Brushes.Transparent, new Thickness(0))
                       .EndSettings<DockPanel>();

            //Add new generic methods SetToCol<K, T> and SetToRow<K, T>?!?

            //Default Settings
            myDockPanel.BeginSettings<DockPanel>()
                .SetTextColorToCol<DockPanel>(0, 1, 2, Brushes.Black)
                .SetTextPadding<DockPanel>(1, 0, new Thickness(3))
                .SetTextPadding<DockPanel>(2, 0, new Thickness(3))
                .SetTextWrapping<DockPanel>(2, 0, System.Windows.TextWrapping.Wrap)
                .SetVerticalAlignment<DockPanel>(1, 0, System.Windows.VerticalAlignment.Center)
                .SetFont<DockPanel>(1, 0, "Arial", 11, FontWeights.DemiBold, FontStyles.Italic)
                .Set<Rectangle, DockPanel>("Fill", Brushes.DarkGreen)
                .Set<Rectangle, DockPanel>("Stroke", Brushes.Black)
                .Set<Rectangle, DockPanel>("StrokeThickness", 3.00D)
                .Set<Rectangle, DockPanel>("Width", 90.00D)
                .Set<Rectangle, DockPanel>("Height", 90.00D)
                .Set<Rectangle, DockPanel>(0, 0, "Fill", Brushes.Red)
                .Set<Button, DockPanel>("HorizontalAlignment", HorizontalAlignment.Center)
                .SetAttachedProperty<Rectangle, DockPanel>(
                "Background", obj => { obj.SetValue(Rectangle.FillProperty, Brushes.LightSeaGreen); })
                .SetItemBorderSettingsFull(double.NaN, double.NaN, Brushes.Silver, new Thickness(2), new CornerRadius(10), new Thickness(3))
                .EndSettings();

            // myDockPanel.SubscribeMouseSingleClick(new MouseButtonEventHandler(my_MouseSingleClick));

            Button myButton = new Button() { Content = "Click to Change Text" };
            Button sortButton = new Button() { Content = "Click to Sort Labels" };
            GridSettings compositeGridSettings = myDockPanel.CompositeGridDimensions<DockPanel>(
                428D, double.NaN, 1, 2, .5, GridUnitType.Star, .5, GridUnitType.Star);

            //Vary the dimensions of the Composite Grid for this particular composite
            myDockPanel.BeginComposite(dockguid0)
                .AddExisting<Button, DockPanel>(0, 0, myButton)
                .SubscribeEventOnChild<Button, DockPanel, RoutedEventArgs>(0, 0, "Click", (myButton_Click))
                .AddExisting<Button, DockPanel>(0, 1, sortButton)
                .SubscribeEventOnChild<Button, DockPanel, RoutedEventArgs>(0, 1, "Click", (sortButton_Click))
                .EndComposite(new DockArgs(Dock.Top), compositeGridSettings);           

            Border handleToBdr = myDockPanel.BeginComposite(dockguid1)
                .AddAnything<Rectangle, DockPanel>(0, 0)
                .AddText(1, 0, "Hello World 1a")
                .AddText(2, 0, "This is text at Row 2 Column 0")
                .EndComposite(new DockArgs(Dock.Left));

            myDockPanel.BeginComposite(dockguid2)
                .AddAnything<Rectangle, DockPanel>(0, 0)
                .AddText(1, 0, "Click to remove.")
                .AddText(2, 0, "This is text at Row 2 Column 0")
                .EndComposite(new DockArgs(Dock.Right))
                .SubscribeEventOnBorder<DockPanel, MouseButtonEventArgs>(dockguid2,
                "PreviewMouseLeftButtonDown", (my_MouseSingleClick));

            //myDockPanel.UnsubscribeChildEventsByKey(dockguid0);
            //Default settings can still be changed at runtime for a particular composite
            myDockPanel.GetTextBlock(dockguid2, 1, 0).VerticalAlignment = VerticalAlignment.Top;

            myDockPanel.GetContainerFromComposite<Grid, DockPanel>(dockguid1).Background = Brushes.Lavender;
            Border xbool = myDockPanel.GetCompositeFromParent<DockPanel>(dockguid1);
            //   var zzz = myDockPanel.GetCompositeLookupDictionary<DockPanel>();
            //  handleToBdr.Background = Brushes.Lavender;

            this.BeginComposite<UserControl>(_userCtlGuid)
               .AddLabel<UserControl>(0, 0, "Tab Item 6 -  Dock Panel Example", Brushes.Purple)
               .AddExisting<DockPanel, UserControl>(0, 1, myDockPanel)
               .EndComposite<UserControl, UserControlArgs>(null);

            //1. Create a Prototype
            Label prototypeLabel = new Label();

            //2. Create a Constructor Action To Be Run Against Each Generated (Cloned) Element. This is IMPORTANT for Initialization and Settings!
            Func<int, Label, Dock> myConstructor = (j, prot) =>
            {
                prot.Visibility = Visibility.Visible; prot.MinHeight = 100D; prot.MinWidth = 100D; prot.BorderBrush = Brushes.Black; 
                prot.BorderThickness = new Thickness(2); prot.Background = Brushes.LightSeaGreen; 
                Dock dockResult = Dock.Bottom; 
                if (j == 1) { dockResult = Dock.Left; } 
                return dockResult; 
            };

            GridSettings compositeGridSettings2 = myDockPanel.CompositeGridDimensions<DockPanel>(double.NaN, double.NaN, 1, 3, 0.33, GridUnitType.Star, 100, GridUnitType.Auto);
            
            //3. Generate the Elements Based On the Prototype and Add Composites to DockPanel According to Range Provided and Dock Arg returned in Constructor
            List<string> keys2 = Generators.AddComposites<Label>(myDockPanel, 0, 0, 1, 3, prototypeLabel, myConstructor, compositeGridSettings2);

            //4. Manipulate Each Individual Element After the Fact, If Desired
            Label b1 = myDockPanel.GetChildViaLookupDictionary<Label, DockPanel>(keys2[0].ToString(), 0, 0);
            b1.Content = "1";
            Label b2 = myDockPanel.GetChildViaLookupDictionary<Label, DockPanel>(keys2[1].ToString(), 0, 0);
            b2.Content = "2";
            Label b3 = myDockPanel.GetChildViaLookupDictionary<Label, DockPanel>(keys2[2].ToString(), 0, 0);
            b3.Content = "34";


            //Add a Composite with 3 Child Labels
            int indx2 = 0;
            Label prototypeLabel2 = new Label() { Height = 43D, Width = 43D, Background = Brushes.Aqua };
            Action<int, Label> labelConstructor2 = (f, lbl) => { indx2++; if (f == 1) { lbl.Background = Brushes.Green; } lbl.Foreground = Brushes.White; lbl.Content = indx2.ToString(); };
            Generators.AddChildrenInColumnRange(myDockPanel, 0, 0, 2, prototypeLabel2, labelConstructor2);
        }
    }
}
